package cn.jinjimi.domain.data.imp;

import cn.jinjimi.domain.data.Store;
import cn.jinjimi.domain.data.net.API;
import cn.jinjimi.domain.data.net.PosterService;
import cn.jinjimi.domain.data.dto.PosterForShareDto;
import cn.jinjimi.domain.data.dto.PosterTemplateDto;
import cn.jinjimi.domain.data.dto.PostersInfoDto;
import cn.jinjimi.domain.store.PosterStore;
import cn.jinjimi.app.poster.bean.PosterMarkDto;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import retrofit2.Response;

/**
 * Created by lforxeverc on 16/9/27.
 */

public class PosterStoreImp extends Store implements PosterStore {
    @Override
    public PosterTemplateDto fetchTemplatesCategories(int parentCode) throws IOException {
        Map<String, Object> map = new HashMap<>();
        if (parentCode != -1) {
            map.put("parent_code", parentCode);
        }
        Response<PosterTemplateDto> data = API.create(PosterService.class).fetchTemplatesCategories(map).execute();
        return data == null ? null : data.body();
    }

    @Override
    public PostersInfoDto fetchPosterTemplates(final Map<String, Object> map) throws IOException {

        Response<PostersInfoDto> resp = API.create(PosterService.class).fetchPosterTemplates(map).execute();
        PostersInfoDto data = getHttpState(resp, new PostersInfoDto());
        return data;
    }

    @Override
    public void sendShareTemplate(int templateId) throws IOException {
        Response<Void> data = API.create(PosterService.class).sendShareTemplate(templateId).execute();
    }

    @Override
    public PosterForShareDto fetchPosterShareMsg(boolean forceRefresh, int templateId) throws IOException {
        Map<String, Object> map = new HashMap<>();
        map.put("force_refresh", forceRefresh);
        map.put("template_id", templateId);
        Response<PosterForShareDto> data = API.create(PosterService.class).fetchPosterShareMsg(map).execute();
        return data == null ? null : data.body();
    }

    @Override
    public PosterMarkDto fetchPosterMarkInfo(int templateId, boolean forceRefresh) throws IOException {
        return getHttpState(API.create(PosterService.class).fetchPosterMarkInfo(templateId, forceRefresh).execute(), new PosterMarkDto());
    }
}
